use mouse_rs::{types::keys::Keys, Mouse};
use ftp::FtpStream;
use std::path::Path;
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[tauri::command]
fn auto_click() {
    let mouse = Mouse::new();
    // 按下左键 → 延迟 → 释放左键
    mouse.press(&Keys::LEFT).expect("Failed to press");
    std::thread::sleep(std::time::Duration::from_millis(50)); // 模拟点击时长
    mouse.release(&Keys::LEFT).expect("Failed to release");
    println!("点击完成");
}
#[tauri::command]
fn upload(file_path: &str, file_name: &str)-> Result<(), String> {
    let server = "172.16.102.20:21";
	let username = "tjzhanglei";
	let password = "Sd89738960!!@@";
	// 连接 FTP 服务器
	let mut ftp_stream = FtpStream::connect(server)
			.map_err(|e| format!("FTP连接失败: {}", e))?;
	ftp_stream.login(username, password)
			.map_err(|e| format!("登录失败: {}", e))?;
	ftp_stream.cwd("/")
			.map_err(|e| format!("切换目录失败: {}", e))?;

	let path = Path::new(&file_path);
	let mut file = std::fs::File::open(path)
			.map_err(|e| format!("打开文件失败: {}", e))?;
	// 上传文件
	ftp_stream.put(&file_name, &mut file)
			.map_err(|e| format!("上传失败: {}", e))?;
	// 关闭连接	
	ftp_stream.quit()
			.map_err(|e| format!("连接关闭失败: {}", e))?;
	Ok(())
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_global_shortcut::Builder::new().build())
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![greet, auto_click, upload])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
